package br.com.padtec.software.snmp.challenge;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import br.com.padtec.software.snmp.challenge.SnmpUtils.NeStatus;
import br.com.padtec.software.snmp.challenge.SnmpUtils.TruthValue;
import com.adventnet.snmp.mibs.MibOperations;
import com.adventnet.snmp.snmp2.SnmpVarBind;
import com.adventnet.snmp.snmp2.UDPProtocolOptions;

public class Viewer extends JFrame {
  private static final long serialVersionUID = 1L;

  private final JPanel contentPanel = new JPanel();
  private JLabel partValue;
  private JLabel serialValue;
  private JLabel hardValue;
  private JLabel firmValue;
  private JLabel slotValue;
  private JLabel statusValue;
  private JLabel laserValue;
  private JLabel nomeValue;
  private JLabel labelNomeDescr;
  private JLabel lblNewLabel;
  private JLabel lblSerial;
  private JLabel lblHard;
  private JLabel lblFirm;
  private JLabel lblSlot;
  private JLabel lblLaser;
  private JLabel lblStatus;
  private JButton btnAlterarSlot;
  private JButton btnAlterarLaser;
  private JButton btnAlterarStatus;

  public void updateData(SnmpBean bean) {
    partValue.setText(bean.getNePartNUmber());
    serialValue.setText(bean.getNeSerialNumber());
    hardValue.setText(bean.getNeHardwareVersion());
    firmValue.setText(bean.getNeFirmwareVersion());
    slotValue.setText(bean.getNeSlot());

    String neStatusStr = bean.getNeStatus();
    NeStatus neStatus = NeStatus.getById(new Integer(neStatusStr));
    statusValue.setText(neStatus.label);

    String laserStatusStr = bean.getNeLaserEnabled();
    TruthValue laserStatus = TruthValue.getById(new Integer(laserStatusStr));
    laserValue.setText(laserStatus.label);

    nomeValue.setText(bean.getNeName());

    if (neStatus.equals(NeStatus.deactivated)) {
      btnAlterarLaser.setEnabled(false);
      btnAlterarSlot.setEnabled(false);
    } else {
      btnAlterarLaser.setEnabled(true);
      btnAlterarSlot.setEnabled(true);

    }

  }

  public Viewer(MibOperations mibOps, SnmpServer snmpServer, UDPProtocolOptions remoteHost) throws HeadlessException {
    super();
    init();
    this.cmdSender = new SnmpCommandSender();
    this.mibOps = mibOps;
    this.snmpServer = snmpServer;
    this.remoteHost = remoteHost;

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  private SnmpCommandSender cmdSender;
  private MibOperations mibOps;
  private SnmpServer snmpServer;
  private UDPProtocolOptions remoteHost;

  /**
   * Create the dialog.
   */
  public void init() {
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    GridBagLayout gbl_contentPanel = new GridBagLayout();
    gbl_contentPanel.columnWidths = new int[] { 47, 47, 102, 0, 47, 47, 47, 47, 47, 47, 0 };
    gbl_contentPanel.rowHeights = new int[] { 23, 0, 0, 0, 0, 0, 0, 0, 0 };
    gbl_contentPanel.columnWeights = new double[] { 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
    gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
    contentPanel.setLayout(gbl_contentPanel);

    labelNomeDescr = new JLabel("Nome:");
    GridBagConstraints gbc_labelNomeDescr = new GridBagConstraints();
    gbc_labelNomeDescr.anchor = GridBagConstraints.SOUTHEAST;
    gbc_labelNomeDescr.insets = new Insets(0, 0, 5, 5);
    gbc_labelNomeDescr.gridx = 1;
    gbc_labelNomeDescr.gridy = 0;
    contentPanel.add(labelNomeDescr, gbc_labelNomeDescr);

    nomeValue = new JLabel("");
    GridBagConstraints gbc_nomeValue = new GridBagConstraints();
    gbc_nomeValue.fill = GridBagConstraints.BOTH;
    gbc_nomeValue.insets = new Insets(0, 0, 5, 5);
    gbc_nomeValue.gridx = 2;
    gbc_nomeValue.gridy = 0;
    contentPanel.add(nomeValue, gbc_nomeValue);

    lblNewLabel = new JLabel("Part:");
    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
    gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
    gbc_lblNewLabel.gridx = 1;
    gbc_lblNewLabel.gridy = 1;
    contentPanel.add(lblNewLabel, gbc_lblNewLabel);
    partValue = new JLabel("");
    GridBagConstraints gbc_partValue = new GridBagConstraints();
    gbc_partValue.gridwidth = 2;
    gbc_partValue.fill = GridBagConstraints.BOTH;
    gbc_partValue.insets = new Insets(0, 0, 5, 5);
    gbc_partValue.gridx = 2;
    gbc_partValue.gridy = 1;
    contentPanel.add(partValue, gbc_partValue);

    lblSerial = new JLabel("Serial:");
    GridBagConstraints gbc_lblSerial = new GridBagConstraints();
    gbc_lblSerial.anchor = GridBagConstraints.EAST;
    gbc_lblSerial.insets = new Insets(0, 0, 5, 5);
    gbc_lblSerial.gridx = 1;
    gbc_lblSerial.gridy = 2;
    contentPanel.add(lblSerial, gbc_lblSerial);
    serialValue = new JLabel("");
    GridBagConstraints gbc_serialValue = new GridBagConstraints();
    gbc_serialValue.fill = GridBagConstraints.BOTH;
    gbc_serialValue.insets = new Insets(0, 0, 5, 5);
    gbc_serialValue.gridx = 2;
    gbc_serialValue.gridy = 2;
    contentPanel.add(serialValue, gbc_serialValue);

    lblHard = new JLabel("Hard: ");
    GridBagConstraints gbc_lblHard = new GridBagConstraints();
    gbc_lblHard.anchor = GridBagConstraints.EAST;
    gbc_lblHard.insets = new Insets(0, 0, 5, 5);
    gbc_lblHard.gridx = 1;
    gbc_lblHard.gridy = 3;
    contentPanel.add(lblHard, gbc_lblHard);
    hardValue = new JLabel("");
    GridBagConstraints gbc_hardValue = new GridBagConstraints();
    gbc_hardValue.fill = GridBagConstraints.BOTH;
    gbc_hardValue.insets = new Insets(0, 0, 5, 5);
    gbc_hardValue.gridx = 2;
    gbc_hardValue.gridy = 3;
    contentPanel.add(hardValue, gbc_hardValue);

    lblFirm = new JLabel("Firm:");
    GridBagConstraints gbc_lblFirm = new GridBagConstraints();
    gbc_lblFirm.anchor = GridBagConstraints.EAST;
    gbc_lblFirm.insets = new Insets(0, 0, 5, 5);
    gbc_lblFirm.gridx = 1;
    gbc_lblFirm.gridy = 4;
    contentPanel.add(lblFirm, gbc_lblFirm);
    firmValue = new JLabel("");
    GridBagConstraints gbc_firmValue = new GridBagConstraints();
    gbc_firmValue.fill = GridBagConstraints.BOTH;
    gbc_firmValue.insets = new Insets(0, 0, 5, 5);
    gbc_firmValue.gridx = 2;
    gbc_firmValue.gridy = 4;
    contentPanel.add(firmValue, gbc_firmValue);

    lblSlot = new JLabel("Slot:");
    GridBagConstraints gbc_lblSlot = new GridBagConstraints();
    gbc_lblSlot.anchor = GridBagConstraints.EAST;
    gbc_lblSlot.insets = new Insets(0, 0, 5, 5);
    gbc_lblSlot.gridx = 1;
    gbc_lblSlot.gridy = 5;
    contentPanel.add(lblSlot, gbc_lblSlot);
    slotValue = new JLabel("");
    GridBagConstraints gbc_slotValue = new GridBagConstraints();
    gbc_slotValue.fill = GridBagConstraints.BOTH;
    gbc_slotValue.insets = new Insets(0, 0, 5, 5);
    gbc_slotValue.gridx = 2;
    gbc_slotValue.gridy = 5;
    contentPanel.add(slotValue, gbc_slotValue);

    btnAlterarSlot = new JButton("Alterar");
    btnAlterarSlot.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        alterarSlot();
      }
    });
    GridBagConstraints gbc_btnAlterarSlot = new GridBagConstraints();
    gbc_btnAlterarSlot.insets = new Insets(0, 0, 5, 5);
    gbc_btnAlterarSlot.gridx = 5;
    gbc_btnAlterarSlot.gridy = 5;
    contentPanel.add(btnAlterarSlot, gbc_btnAlterarSlot);

    lblLaser = new JLabel("Laser:");
    GridBagConstraints gbc_lblLaser = new GridBagConstraints();
    gbc_lblLaser.anchor = GridBagConstraints.EAST;
    gbc_lblLaser.insets = new Insets(0, 0, 5, 5);
    gbc_lblLaser.gridx = 1;
    gbc_lblLaser.gridy = 6;
    contentPanel.add(lblLaser, gbc_lblLaser);
    laserValue = new JLabel("");
    GridBagConstraints gbc_laserValue = new GridBagConstraints();
    gbc_laserValue.fill = GridBagConstraints.BOTH;
    gbc_laserValue.insets = new Insets(0, 0, 5, 5);
    gbc_laserValue.gridx = 2;
    gbc_laserValue.gridy = 6;
    contentPanel.add(laserValue, gbc_laserValue);

    btnAlterarLaser = new JButton("Alterar");
    btnAlterarLaser.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        alterarLaser();
      }
    });
    GridBagConstraints gbc_btnAlterarLaser = new GridBagConstraints();
    gbc_btnAlterarLaser.insets = new Insets(0, 0, 5, 5);
    gbc_btnAlterarLaser.gridx = 5;
    gbc_btnAlterarLaser.gridy = 6;
    contentPanel.add(btnAlterarLaser, gbc_btnAlterarLaser);

    lblStatus = new JLabel("Status:");
    GridBagConstraints gbc_lblStatus = new GridBagConstraints();
    gbc_lblStatus.anchor = GridBagConstraints.EAST;
    gbc_lblStatus.insets = new Insets(0, 0, 0, 5);
    gbc_lblStatus.gridx = 1;
    gbc_lblStatus.gridy = 7;
    contentPanel.add(lblStatus, gbc_lblStatus);
    statusValue = new JLabel("");
    GridBagConstraints gbc_statusValue = new GridBagConstraints();
    gbc_statusValue.insets = new Insets(0, 0, 0, 5);
    gbc_statusValue.fill = GridBagConstraints.BOTH;
    gbc_statusValue.gridx = 2;
    gbc_statusValue.gridy = 7;
    contentPanel.add(statusValue, gbc_statusValue);

    btnAlterarStatus = new JButton("Alterar");
    btnAlterarStatus.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        alterarStatus();
      }
    });
    GridBagConstraints gbc_btnAlterarStatus = new GridBagConstraints();
    gbc_btnAlterarStatus.insets = new Insets(0, 0, 0, 5);
    gbc_btnAlterarStatus.gridx = 5;
    gbc_btnAlterarStatus.gridy = 7;
    contentPanel.add(btnAlterarStatus, gbc_btnAlterarStatus);
  }

  private void alterarSlot() {
    try {
      String slot = JOptionPane.showInputDialog("Digite o slot desejado");

      if (slot != null) {
        String columnOid = ".1.3.6.1.4.1.1.1.1.1.1.1.6";
        String targetOid = columnOid + "." + partValue.getText() + "." + serialValue.getText();
        SnmpVarBind varBind = cmdSender.createVarBind(mibOps, columnOid, slot, targetOid);

        cmdSender.sendSet(mibOps, snmpServer, 1, remoteHost, "", SnmpUtils.PACKET_TIMEOUT_IN_MILLIS, varBind);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void alterarLaser() {
    try {
      JComboBox combo = new JComboBox();
      combo.addItem(TruthValue.TRUE);
      combo.addItem(TruthValue.FALSE);
      TruthValue selected = TruthValue.getByLabel(laserValue.getText());
      combo.setSelectedItem(selected);
      Object[] items = { "Digite o status de laser desejado", combo };
      int resp = JOptionPane.showConfirmDialog(this, items, "", JOptionPane.OK_CANCEL_OPTION);
      if (resp == JOptionPane.OK_OPTION) {
        String laserStatus = String.valueOf(((TruthValue) combo.getSelectedItem()).id);
        String columnOid = ".1.3.6.1.4.1.1.1.1.1.1.1.8";
        String targetOid = columnOid + "." + partValue.getText() + "." + serialValue.getText();
        SnmpVarBind varBind = cmdSender.createVarBind(mibOps, columnOid, laserStatus, targetOid);

        cmdSender.sendSet(mibOps, snmpServer, 1, remoteHost, "", SnmpUtils.PACKET_TIMEOUT_IN_MILLIS, varBind);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void alterarStatus() {
    try {
      JComboBox combo = new JComboBox();
      combo.addItem(NeStatus.activated);
      combo.addItem(NeStatus.testing);
      combo.addItem(NeStatus.deactivated);
      NeStatus selected = NeStatus.getByLabel(statusValue.getText());
      combo.setSelectedItem(selected);
      Object[] items = { "Digite o status desejado", combo };

      int resp = JOptionPane.showConfirmDialog(this, items, "", JOptionPane.OK_CANCEL_OPTION);
      if (resp == JOptionPane.OK_OPTION) {
        String status = String.valueOf(((NeStatus) combo.getSelectedItem()).id);
        String columnOid = ".1.3.6.1.4.1.1.1.1.1.1.1.7";
        String targetOid = columnOid + "." + partValue.getText() + "." + serialValue.getText();
        SnmpVarBind varBind = cmdSender.createVarBind(mibOps, columnOid, status, targetOid);

        cmdSender.sendSet(mibOps, snmpServer, 1, remoteHost, "", SnmpUtils.PACKET_TIMEOUT_IN_MILLIS, varBind);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
